import entity.MapPointXmlEntity;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PraseXml {

    public static void main(String args[]) {
        try {
            List<MapPointXmlEntity> pointXmlEntityList = praseXMl();
            int s=1;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public Document parse(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;
    }

    public static List<MapPointXmlEntity> praseXMl() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("/Users/mk/Documents/无人机/裕南线.kml"));
        Element rootElement = document.getRootElement().element("Document");
        String createName = rootElement.element("name").getText();
        Element folder = rootElement.element("Folder");
        Iterator<Element> elementIterator1 = folder.elementIterator("Placemark");
        List<MapPointXmlEntity> pointXmlEntityList=new ArrayList<MapPointXmlEntity>();

        List<Node> content1 = folder.content();


        for (Iterator<Element> elementIterator = folder.elementIterator("Placemark"); elementIterator.hasNext(); ) {

            Element element = elementIterator.next();
            String name = element.element("name").getTextTrim();
//            String description = element.element("description").getTextTrim();
            Element style = element.element("Style");
            List<Node> content = style.content();
            for (int i=0;i<content.size();i++){
                String styleName = content.get(i).getName();
                if(styleName!=null&&styleName.equals("IconStyle")){
                    String coordinates = element.element("Point").element("coordinates").getTextTrim();
                    String[] split = coordinates.split(",");
                    //经度
                    String longitude=split[0];
                    //纬度
                    String latitude=split[1];
                    //海拔高度
                    String altitude=split[2];

                    MapPointXmlEntity mapPointXmlEntity = new MapPointXmlEntity();
                    mapPointXmlEntity.setCreateName(createName);
                    mapPointXmlEntity.setPlaceNamel(name);
//            mapPointXmlEntity.setPlaceDescription(description);
                    mapPointXmlEntity.setLongitude(longitude);
                    mapPointXmlEntity.setLatitude(latitude);
                    mapPointXmlEntity.setAltitude(altitude);
                    pointXmlEntityList.add(mapPointXmlEntity);
                }
            }

        }

        return pointXmlEntityList;
    }
}
